package com.ruoyi.teacher.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;

import com.ruoyi.teacher.domain.TExam;
import com.ruoyi.teacher.domain.THomework;
import com.ruoyi.teacher.domain.TPaper;
import com.ruoyi.teacher.domain.TPaperQuestion;
import com.ruoyi.teacher.mapper.TExamMapper;
import com.ruoyi.teacher.mapper.THomeworkMapper;
import com.ruoyi.teacher.mapper.TPaperQuestionMapper;
import com.ruoyi.teacher.vo.TPaperVo;
import com.ruoyi.teacher.mapper.TPaperMapper;
import com.ruoyi.teacher.service.ITPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 试卷Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-03
 */
@Service
public class TPaperServiceImpl extends ServiceImpl<TPaperMapper, TPaper> implements ITPaperService {

    @Override
    public TableDataInfo<TPaperVo> queryList(TPaperVo entity) {
        return PageUtils.buildDataInfo(this.baseMapper.queryList(PageUtils.buildPage(), entity));
    }

    @Override
    public List<TPaperVo> queryAll(TPaperVo entity) {
        return this.baseMapper.queryList(entity);
    }

    @Override
    public TPaperVo queryById(Long id) {
        return this.baseMapper.queryById(id);
    }

    @Autowired
    TExamMapper examMapper;
    @Autowired
    THomeworkMapper homeworkMapper;
    @Autowired
    TPaperQuestionMapper paperQuestionMapper;
    @Override
    public void savePaper(TPaperVo entity) {
        // 验证试卷是否存在
        TPaper paper = entity.getPaper();
        if (paper == null) {
            throw new IllegalArgumentException("试卷信息不能为空");
        }

        // 检查试卷是否已发布到考试
        QueryWrapper<TExam> eqw = new QueryWrapper<>();
        eqw.eq("p_id", paper.getId());
        Integer examCount = examMapper.selectCount(eqw);
        if (examCount != 0) {
            throw new RuntimeException("该试卷已发布了【" + examCount + "】场考试，无法更改试卷");
        }

        // 检查试卷是否已发布到作业
        QueryWrapper<THomework> hqw = new QueryWrapper<>();
        hqw.eq("p_id", paper.getId());
        Integer homeworkCount = homeworkMapper.selectCount(hqw);
        if (homeworkCount != 0) {
            throw new RuntimeException("该试卷已发布了【" + homeworkCount + "】场作业，无法更改试卷");
        }

        // 插入或更新试卷信息
        if (paper.getId() == null) {
            baseMapper.insert(paper);
        } else {
            baseMapper.updateById(paper);

            // 删除所有关联的试卷问题
            QueryWrapper<TPaperQuestion> qqw = new QueryWrapper<>();
            qqw.eq("p_id", paper.getId());
            paperQuestionMapper.delete(qqw);
        }

//        获取paperquestion的list
        List<List<TPaperQuestion>> paperQuestionListList = entity.getPaperQuestionListList();
        for (List<TPaperQuestion> pql : paperQuestionListList) {
            int count1 = 0;
            for (TPaperQuestion pq : pql) {
                System.err.println(paper.getId());
                pq.setpId(paper.getId());
//                pq.setqId();
                pq.setOrderNum(++count1);
                paperQuestionMapper.insert(pq);
            }
        }
    }
}
